package org.csu.mypetstore.web.servlets;


import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Log;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

//进行注册，注册成功则转到主页面，否则显示提示信息重新注册
public class RegisterSuccessfulServlet extends HttpServlet {
    private static final String NEW_ACCOUNT_SUCCESSFUL = "/WEB-INF/jsp/catalog/Main.jsp";
    private static final String REGISTER = "/WEB-INF/jsp/account/NewAccountForm.jsp";
    private Account account;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取输入的信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repeatedPassword = request.getParameter("repeatedPassword");
        String valistr = request.getParameter("valistr");
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String address1 = request.getParameter("address1");
        String address2 = request.getParameter("address2");
        String city = request.getParameter("city");
        String state = request.getParameter("state");
        String zip = request.getParameter("zip");
        String country = request.getParameter("country");
        String languages = request.getParameter("languagePreference");
        String categories = request.getParameter("favouriteCategoryId");

        boolean listOption;
        if (request.getParameter("listOption") != null) {
            listOption = true;
        } else {
            listOption = false;
        }

        boolean bannerOption;
        if (request.getParameter("bannerOption") != null) {
            bannerOption = true;
        } else {
            bannerOption = false;
        }

        System.out.println(username+" "+password+" "+firstName+" "+lastName+" "+email+" "+phone+" "+address1+" "+address2+" "+city+" "+state+" "+zip+" "+country+" "+languages+" "+categories);
        //与数据库交互插入数据
        AccountService accountService = new AccountService();

        account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        account.setFirstName(firstName);
        account.setLastName(lastName);
        account.setEmail(email);
        account.setPhone(phone);
        account.setAddress1(address1);
        account.setAddress2(address2);
        account.setCity(city);
        account.setState(state);
        account.setZip(zip);
        account.setCountry(country);
        account.setLanguagePreference(languages);
        account.setFavouriteCategoryId(categories);
        account.setListOption(listOption);
        account.setBannerOption(bannerOption);

        if(username==null){
            System.out.println("用户名为空");
        }else{
            //判断插入是否成功
            if (accountService.insertAccount(account)) {
                HttpSession session = request.getSession();
                session.setAttribute("account", account);

                //时间
                SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String datetime = tempDate.format(new java.util.Date());
                //信息
                String info = "用户"+account.getUsername()+"注册成功";
                Log log = new Log(account.getUsername(),datetime,info);
                //添加log
                new AccountService().addLog(log);
                request.getRequestDispatcher(NEW_ACCOUNT_SUCCESSFUL).forward(request, response);
            } else {
                //提醒已存在该名字的用户
                System.out.println("插入新用户失败");
                request.getRequestDispatcher(REGISTER).forward(request, response);
            }
        }
    }
}
